package com.winit;

import com.winit.common.SignGenerator;
import com.winit.employeInfo.model.Employee;
import com.winit.employeeDataExtract.EmployeeDataExtractor;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.util.List;


public class DataExtract {
    public static CookieStore cookieStore = new BasicCookieStore();
    public static CloseableHttpClient httpCilent = HttpClients.custom().setDefaultCookieStore(cookieStore).build();


    @Test
    public void getSign(){
        System.out.println(SignGenerator.getSign());
    }

    @Test
    public void extractData() throws Exception{
        String url = "https://www.onehrcloud.com/bf-portal/portal/sv/onehrcloud/215220";
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000) // 设置连接超时时间
                .setConnectionRequestTimeout(50000) // 设置请求超时时间
                .setSocketTimeout(50000).setRedirectsEnabled(true)// 默认允许自动重定向
                .build();
        URI uri = new URIBuilder()
                .setScheme("https")
                .setHost("www.onehrcloud.com")
                .setPath("/bf-portal/portal/sv/onehrcloud/215220")
                .setParameter("type", "emp")
                .setParameter("sign", SignGenerator.getSign())
                .setParameter("language", "1")
                .setParameter("timeOffSet", "2017-02-19 22:22:22")
                .build();
        HttpGet httpGet = new HttpGet(uri);
        httpGet.setConfig(requestConfig);
        String srtResult =null;
        int StatusCode=404;
        try {
            HttpResponse httpResponse = httpCilent.execute(httpGet);
            StatusCode=httpResponse.getStatusLine().getStatusCode();
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                srtResult = EntityUtils.toString(httpResponse.getEntity());// 获得返回的结果
//                logger.info("get/"+StatusCode+":"+srtResult);
//                return srtResult;
                System.out.println(srtResult);
            } else {
                srtResult = EntityUtils.toString(httpResponse.getEntity());// 获得返回的结果
                System.out.println(srtResult);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }

    public void tt() throws Exception{

//        HttpGet httpget = new HttpGet(uri);
    }

    @Test
    public void count() throws Exception{
        List<Employee> employeeList = EmployeeDataExtractor.extractEmployee();
        System.out.println(employeeList.size());
    }
}
